package cn.edu.xmu.oomall.comment.dao;



import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.mapper.RedisUtil;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.util.CloneFactory;
import cn.edu.xmu.oomall.comment.dao.bo.OrderItem;
import cn.edu.xmu.oomall.comment.mapper.OrderItemPoMapper;
import cn.edu.xmu.oomall.comment.mapper.po.OrderItemPo;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.util.Optional;

@Repository
@RefreshScope
@RequiredArgsConstructor
public class OrderItemDao {

    private final static Logger logger = LoggerFactory.getLogger(OrderItemDao.class);

    private final CommentDao commentDao;

    private final OrderItemPoMapper orderItemPoMapper;

    @Value("${oomall.comment.shop-logistics.timeout}")
    private int timeout;
    private final RedisUtil redisUtil;

    public void build(OrderItem bo) {
        bo.setOrderItemDao(this);
        bo.setCommentDao(commentDao);
    }

    public OrderItem findById(Long id) {
        logger.debug("findById: id = {}", id);
        Optional<OrderItemPo> po = orderItemPoMapper.findById(id);
        if(po.isEmpty()){
            throw new BusinessException(ReturnNo.UNKNOWN_ORDER_ITEM, String.format(ReturnNo.UNKNOWN_ORDER_ITEM.getMessage(), id));
        }
        OrderItem bo = CloneFactory.copy(new OrderItem(), po.get());
        this.build(bo);  // 设置dao引用
        return bo;
    }
}

